//
//  TopViewHintControl.m
//  CheFu365
//
//  Created by safiri on 2018/1/15.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "ZSAutoScrollHintLabelView.h"

@interface ZSAutoScrollHintLabelView ()

@property (nonatomic ,weak) UIView *contentView;

@property (nonatomic ,weak) UIImageView *trumpet;

@property (nonatomic ,weak) UIButton *closeButton;

@end

@implementation ZSAutoScrollHintLabelView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _space = 8;
        [self configUI];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _space = 8;
        [self configUI];
    }
    return self;
}

- (void)configUI {
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor colorWithRed:254/255.0 green:252/255.0 blue:237/255.0 alpha:1];
    [self addSubview:contentView];
    self.contentView= contentView;
    
    UIImageView *trumpet = [[UIImageView alloc] init];
    if (self.trumpetImage == nil) {
        self.trumpetImage = [self getDefaultImage:@"autoScrollHintTrumpet@2x"];
    }
    trumpet.image = self.trumpetImage;
    [contentView addSubview:trumpet];
    self.trumpet = trumpet;
    
    CBAutoScrollLabel *contentLab = [[CBAutoScrollLabel alloc] init];
    [contentLab setFont:[UIFont systemFontOfSize:15]];
    [contentLab setTextColor:[UIColor colorWithRed:255/255.0 green:133/255.0 blue:60/255.0 alpha:1]];
    [contentView addSubview:contentLab];
    self.contentLab = contentLab;
    
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (self.closeButtonImage == nil) {
        self.closeButtonImage = [self getDefaultImage:@"autoScrollHintClose@2x"];
    }
    [closeBtn setImage:self.closeButtonImage forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:closeBtn];
    self.closeButton = closeBtn;
}

- (UIImage *)getDefaultImage:(NSString *)imageName {
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[ZSAutoScrollHintLabelView class]] pathForResource:@"ZSAutoScrollHintLabelView" ofType:@"bundle"]];
    NSString *path = [bundle pathForResource:imageName ofType:@"png"];
    return [UIImage imageWithContentsOfFile:path];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    CGFloat space = self.space;
    CGFloat trumpetImageWidth = 16;
    CGFloat trumpetImageHeight = 16;
    if (self.trumpetImage) {
        trumpetImageWidth = self.trumpetImage.size.width;
        trumpetImageHeight = self.trumpetImage.size.height;
    }
    CGFloat closeButtonWidth = 16;
    CGFloat closeButtonHeight = 16;
    if (self.closeButtonImage) {
        closeButtonWidth = self.closeButtonImage.size.width;
        closeButtonHeight = self.closeButtonImage.size.height;
    }
    
    self.contentView.frame = self.bounds;
    self.trumpet.frame = CGRectMake(space, (height-trumpetImageHeight)/2, trumpetImageWidth, trumpetImageHeight);
    self.contentLab.frame = CGRectMake(CGRectGetMaxX(self.trumpet.frame) + space, (height-22)/2, width-trumpetImageWidth-closeButtonWidth-space*4, 22);
    self.closeButton.frame = CGRectMake(CGRectGetMaxX(self.contentLab.frame)+space, (height-closeButtonHeight)/2, closeButtonWidth, closeButtonHeight);
}

- (void)closeClick {
    if ([self.delegate respondsToSelector:@selector(autoScrollHintLabelViewClose)]) {
        [self.delegate autoScrollHintLabelViewClose];
    }
}

- (void)setContentText:(NSString *)contentText {
    _contentText = contentText;
    self.contentLab.text = contentText;
    [self.contentLab scrollLabelIfNeeded];
}

- (void)setContentViewBackgroundColor:(UIColor *)contentViewBackgroundColor {
    _contentViewBackgroundColor = contentViewBackgroundColor;
    self.contentView.backgroundColor = contentViewBackgroundColor;
}

- (void)setContentTextColor:(UIColor *)contentTextColor {
    _contentTextColor = contentTextColor;
    [self.contentLab setTextColor:contentTextColor];
}

- (void)setTrumpetImage:(UIImage *)trumpetImage {
    _trumpetImage = trumpetImage;
    self.trumpet.image = trumpetImage;
}

- (void)setCloseButtonImage:(UIImage *)closeButtonImage {
    _closeButtonImage = closeButtonImage;
    [self.closeButton setImage:self.closeButtonImage forState:UIControlStateNormal];
}
@end
